<?php

namespace App\Tools;

use App\Models\Ems;
use App\Models\User;
use App\Models\UserLog;
use App\Models\UserRechargeLog;

Trait Func
{
    public function returnJson401($status = 0, $message = 'success', $data =  []) 
    {
        $result = [
            'status_code' => 401,
            'status' => 0,
            'message' => '请重新登录',
            'data' => $data
        ];
        return response()->json($result);
    }

    public function returnJson($status = 0, $message = 'success', $data =  [], $trace = []) 
    {
        $result = [
            'status_code' => 200,
            'status' => $status,
            'message' => $message,
            'data' => $data
        ];
        return response()->json($result);
    }

    // 分页查询
    public function pageList($userId, $keywords, $serchTime)
    {
    	return Ems::with('user')
		        ->where(function($query) use($keywords, $userId, $serchTime) {
		            // 关键字
		            if ($keywords) {
		                $query->orWhere('no', $keywords);
		            }
		            // 搜索开结束时间
		        	if ($serchTime) {
		        		$query->whereBetween('get_time', [
		        			$serchTime[0], 
		        			$serchTime[1].' 23:59:59'
		    			]);
		        	}
		            // 用户id
		            if($userId) {
		                $query->where('user_id', $userId);
		            }
		        })
		        ->orderBy('get_time', 'desc');
    }

    // 日志记录
    public function userLogDo($user_id, $data, $type_code)
    {
        UserLog::create([
            'user_id' => $user_id, 
            'desc' => json_encode($data),
            'type_code' => $type_code,
            'created_at' => date('Y-m-d H:i:s')
        ]);
    }

    // 添加充值记录
    public function addUserRechargeLog($user_id, $account_user_id, $add_money)
    {
        UserRechargeLog::create([
            'account_user_id' => $account_user_id,
            'add_money' => $add_money,
            'operat_id' => $user_id,
            'created_at' => date('Y-m-d H:i:s')
        ]);
    }

    //导入统计数据
    public function impTotalDataInsert()
    {
        // 总共使用金额
        
        //总充值金额
        
        // 插入每次的记录
    }

}

